import api from '../../api';
import { message } from 'antd';
import { isZiGuanEvir } from '../../utils/commonFunc';
import statusData from '../../utils/enumeratedValues/statusData';
export default {
  namespace: 'proposalVerifyM',
  state: {
    loading: true,
    columns: [],
    dataSource: [],
    pageInfo: {},
    ProposalStatus: [], //状态
    ProposalType: [], //建议书类型
    info: null, //审批数据
  },
  effects: {
    *getData({ payload, callback }, { call, put, select }) {
      const newPayload = JSON.parse(JSON.stringify(payload)); //删除对象中下拉框的key
      delete newPayload.selectVal;
      let withMAAT = false;
      let response;
      // 判断当前环境是不是资管
      let temp = isZiGuanEvir();
      yield put({
        type: 'loading',
      });
      if (!!temp && temp['iv-user']) withMAAT = true;
      if (withMAAT) {
        response = yield call(api.proposalVerifyM.getTableDataWITHMAAT, newPayload);
      } else {
        response = yield call(api.proposalVerifyM.getTableData, newPayload);
      }
      const datas = payload.selectVal;
      // 表格调用分页的接口
      if (response.code == 0) {
        let changeData = []; //用于接收表格处理数据
        if (response.data.dataSource.length) {
          changeData = response.data.dataSource;
          changeData.map((item, index) => {
            datas.PROPOSAL_STATUS.map((items, indexs) => {
              if (item.proposalStatus == items.code) {
                item.proposalStatuss = item.proposalStatus;
                item.proposalStatus = items.value;
              }
            });
            if (item.proposalType == 'ETF') {
              item.proposalTypes = item.proposalType;
              item.proposalType = '场内(ETF)基金';
            }
            statusData.clinchStatus.map((itemss) => {
              if (item.clinchStatus == itemss.value) {
                item.clinchStatus = itemss.title;
              }
            });
          });
        }
        yield put({
          type: 'getTable',
          payload: {
            columns: response.data.columns,
            dataSource: changeData,
            pageInfo: response.data.pageInfo,
          },
        });
      } else {
        response.msg ? message.error(response.msg) : message.error('查询表格数据失败！');
        yield put({
          type: 'getTable',
        });
      }
    },
    *approveData({ payload, callback }, { call, put }) {
      //审批详情中数据的获取
      let withMAAT = false;
      let response;
      // 判断当前环境是不是资管
      let temp = isZiGuanEvir();
      if (!!temp && temp['iv-user']) withMAAT = true;
      if (withMAAT) {
        response = yield call(api.proposalVerifyM.findOnByproposalIdWITHMAAT, payload);
      } else {
        response = yield call(api.proposalVerifyM.findOnByproposalId, payload);
      }
      if (response.success) {
        callback && callback(response.data);
        yield put({
          type: 'valApprove',
          payload: { ...response.data },
        });
      } else {
        response.message ? message.error(response.message) : message.error('查询审批详情失败');
      }
    },
    //建议书审批
    *proposalAuth({ payload, callback }, { call, put }) {
      let withMAAT = false;
      let response;
      // 判断当前环境是不是资管
      let temp = isZiGuanEvir();
      if (!!temp && temp['iv-user']) withMAAT = true;
      if (withMAAT) {
        response = yield call(api.proposalVerifyM.proposalAuthWITHMAAT, payload);
      } else {
        response = yield call(api.proposalVerifyM.proposalAuth, payload);
      }
      if (response.success) {
        payload.proposalStatus == 'SENT' ? message.success('审批成功') : message.success('拒绝成功');
        callback();
      } else {
        var selfMsg = payload.proposalStatus == 'SENT' ? '审批失败' : '拒绝失败';
        var msg = response ? response.message || selfMsg : selfMsg;
        payload.proposalStatus == 'SENT' ? message.error(msg) : message.error('拒绝失败');
      }
    },
  },
  reducers: {
    loading(state, { payload }) {
      state.loading = true;
      return { ...state };
    },
    getTable(state, { payload }) {
      state.loading = false;
      return { ...state, ...payload };
    },
    valSelect(state, { payload }) {
      state.ProposalStatus = payload.PROPOSAL_STATUS;
      state.ProposalType = payload.PROPOSAL_TYPE;
      return { ...state };
    },
    valApprove(state, { payload }) {
      //审批的详情
      if (state.ProposalStatus.length) {
        state.ProposalStatus.map((item, index) => {
          if (item.code == payload.proposalStatus) {
            payload.proposalStatus = item.value;
          }
        });
        // state.ProposalType.map((items,indexs)=>{
        //     if(items.code == payload.proposalType){
        //         payload.proposalType = items.value
        //     }
        // });
        payload.exchangeCode = payload.exchangecode;
      }
      state.info = payload;
      return { ...state };
    },
  },
};
